/*
 * @Author: 晴鸢. 3013433857@qq.com
 * @Date: 2025-09-03 12:56:30
 * @LastEditors: 晴鸢. 3013433857@qq.com
 * @LastEditTime: 2025-09-08 11:05:48
 * @FilePath: /Learn/sig/mysignal.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include<iostream>
#include<signal.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
using namespace std;
int main()
{
    pid_t id=fork();
    if(id==0)
    {
        int cnt=500;
        while(cnt--)
        {
            cout<<"I am a child process "<<getpid()<<"cnt "<<cnt<<endl;
            sleep(1);
        }        
        exit(0);
    }
    int sta=0;
    pid_t rid=waitpid(id,&sta,0);
    if(rid==id)
    {
        cout<<"child quit info,rid:"<<rid<<"exit code:"<<((sta>>8)&0xFF)
        <<" exit signal:"<<(sta&0x7f)<<" core dump:"<<((sta>>7)&1)<<endl; 
    }
    return 0;
}
/*
void work()
{
    cout<<"print log ..."<<endl;
}

void headler(int signo)
{
    work();
    //cout<<"get a sig,num: "<<signo<<endl;
    //exit(1);
    alarm(5);
}
int main()
{
    signal(14,headler);
    int n=alarm(5);
    while(1)
    {
        //sleep(1);
        cout<<"proc is runnning..."<<endl;
        sleep(1);
    }

    /*
    //signal(8,headler);
    signal(11,headler);
    //cout<<"div before"<<endl;
    cout<<"point error before"<<endl;
    sleep(1);

    int *p=nullptr;
    *p=100;
    //int a=10;
    //异常调用8号信号
    //a/=0;
    //cout<<"div after"<<endl;
    cout<<"point error after"<<endl;
    sleep(1);
    return 0;
    
}
*/
